import java.util.ArrayList;
import java.util.List;

//二叉树的所有路径
//测试链接：https://leetcode.cn/problems/binary-tree-paths/
public class BinaryTreePaths {

    public class TreeNode {
         int val;
         TreeNode left;
         TreeNode right;
         TreeNode() {}
         TreeNode(int val) { this.val = val; }
         TreeNode(int val, TreeNode left, TreeNode right) {
             this.val = val;
             this.left = left;
             this.right = right;
         }
     }

    List<String> ans = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if(root == null) return null;
        dfs(root,"");
        return ans;
    }

    void dfs(TreeNode root, String path) {
        if(root == null ) return;
        if(root.left == null && root.right == null) {  //来到叶子节点 将路径添加进ans
            ans.add(path+root.val);
            return;
        }
        path = path + root.val + "->";
        dfs(root.left,path);
        dfs(root.right,path);
    }
}
